package com.laicunba.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.UserBankDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.RegistryService;
import com.laicunba.service.UserBankService;
import com.laicunba.service.UserService;
import com.laicunba.util.StringUtil;

@Service
public class UserBankServiceImpl extends BaseServiceImpl implements UserBankService {

	@Autowired
	private UserService userService;

	@Autowired
	private RegistryService registryService;

	@Override
	public List<UserBankDO> findByUserId(String userId) {
		return queryDao.selectList("USER_BANK.findByUserId", userId);
	}

	@Override
	public UserBankDO find(String userId, String bankCardNo) {
		Map map = new HashMap();
		map.put("userId", userId);
		map.put("bankCardNo", bankCardNo);
		return queryDao.selectOne("USER_BANK.findByUserIdAndCard", map);
	}

	@Transactional
	@Override
	public boolean canBind(String userId, String bankCardNo) {
		UserBankDO bankDO = find(userId, bankCardNo);
		if (bankDO != null) {
			return true;
		}
		Map map = new HashMap();
		map.put("userId", userId);
		map.put("bankCardNo", bankCardNo);
		return (Integer) queryDao.selectOne("USER_BANK.findOtherUserCard", map) == 0;
	}

	@Override
	public UserBankDO findLastByUserId(String uid) {
		return queryDao.selectOne("USER_BANK.findLastByUserId", uid);
	}

	@Transactional
	@Override
	public boolean canBind(String userId, String pid, String bankCardNo) {
		UserBankDO bankDO = find(userId, bankCardNo);
		if (bankDO != null) {
			// 已绑成功的卡能用
			return true;
		}
		Map map = new HashMap();
		map.put("userId", userId);
		map.put("bankCardNo", bankCardNo);
		if ((Integer) queryDao.selectOne("USER_BANK.findOtherUserCard", map) == 1) {
			// 别人绑的卡不能用
			return false;
		}
		UserDO userDO = userService.get(UserDO.class, userId);
		if (userDO == null) {
			return false;
		} 
		
		//该身份证已被使用，则禁用
	    if(userService.isPidBindByOtherUsr(pid,userId)){
	    	return false;
	    }		
		//新用户
		if(StringUtil.isEmpty(userDO.getPid())){
			return true;
		}
		//非同名卡,Refused
		if(!pid.equalsIgnoreCase(userDO.getPid())){
			return false;
		}

		
		return testNewCardPolicy(userDO.getId(),bankCardNo);
	}

	@Transactional
	@Override
	public UserBankDO findRepaymentCard(String userId) {
		List<UserBankDO> list = findByUserId(userId);
		if (list == null || list.isEmpty()) {
			return null;
		}
		if (list.size() == 1) {
			return list.get(0);
		}
		return queryDao.selectOne("USER_BANK.findRepaymentCard", userId);
	}

	@Override
	public List<UserBankDO> findByUserIdContainDisable(String uid) {
		return queryDao.selectList("USER_BANK.findByUserIdContainDisable", uid);
	}

	@Override
	public boolean disable(String bankCardId) {
		return updateDao.update("USER_BANK.disable", bankCardId) == 1;
	}

	@Override
	public boolean testNewCardPolicy(String userId, String bankCardNo) {
		// 符合绑卡策略的能用
		RegistryDO newCardPolicyDO = registryService.get(RegistryDO.class, "u." + userId + ".newcard.policy");
		String newCardPolicy = newCardPolicyDO!=null?newCardPolicyDO.getVal():"";
		if("-".equals(newCardPolicy)){
			List<UserBankDO> list = findByUserId(userId);
			if (list != null && !list.isEmpty()) {
				// 只能绑一张卡
				return false;
			}
		}
		
		if("*".equals(newCardPolicy)){
			return true;
		}
		
		for(String applyCard : newCardPolicy.split(",")){
			if(applyCard.equals(bankCardNo)){
				return true;
			}
		}
		return false;
	}
}
